package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.domain.ClassLevel;
import com.xjh.t.domain.Lesson;
import com.xjh.t.domain.LessonQuiz;
import com.xjh.t.service.ILessonQuizService;
import com.xjh.t.vo.LessonQuizVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2020-11-05
 */
@RestController
@RequestMapping("/lessonquiz")
public class LessonQuizController {

    @Autowired
    private ILessonQuizService lessonQuizService;
    
    @RequestMapping("/addQuestion")
    public ResultObj addlesson(LessonQuiz question) {


        this.lessonQuizService.save(question);

        return ResultObj.ADD_SUCESS;

    }


    @RequestMapping("/loadAllQuestion")
    public DataGridView loadAlllesson(LessonQuizVo quizVo) {

        IPage<LessonQuiz> page=new Page<>(quizVo.getPage(), quizVo.getLimit());
        QueryWrapper<LessonQuiz> queryWrapper=new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(quizVo.getQuestionDes()), "question_desc",quizVo.getQuestionDes());
        queryWrapper.eq(StringUtils.isNotBlank(quizVo.getPracticeWords()), "practice_words",quizVo.getPracticeWords());


        this.lessonQuizService.page(page, queryWrapper);

/*
        List<LessonQuizVo> quizVos = new ArrayList<>();
        for(LessonQuiz lesson:page.getRecords())
        {
            LessonQuizVo quizVo1 = new LessonQuizVo();
            BeanUtils.copyProperties(lesson,quizVo1);

            ClassLevel classLevel = this.classLevelService.getById(lesson.getBelongLevel());

            quizVo1.setBelongClass(classLevel.getLevelComment());

            quizVos.add(quizVo1);
        }*/

        return new DataGridView(page.getTotal(),page.getRecords());

    }

    @RequestMapping("/updateQuestion")
    public ResultObj updateLesson(LessonQuiz lesson) {

        this.lessonQuizService.updateById(lesson);

        return ResultObj.UPDATE_SUCESS;

    }
    
}

